library(reshape2)
library(dplyr)
library(ggplot2)
library(grid)
library(ggpubr)

######Load DF
DF <- read.csv2("MEA_sample.csv", sep = ";")

DF$Time.Period <-  ifelse(DF$original_year %in% c(1945:1954), "1945-1954",
                             ifelse(DF$original_year %in% c(1955:1964), "1955-1964",
                                    ifelse(DF$original_year %in% c(1965:1974), "1965-1974",
                                                  ifelse(DF$original_year %in% c(1975:1984), "1975-1984",
                                                                ifelse(DF$original_year %in% c(1985:1994), "1985-1994",
                                                                              ifelse(DF$original_year %in% c(1995:2004), "1995-2004",
                                                                                            ifelse(DF$original_year %in% c(2005:2017), "2005-2015", "Error")))))))

DF$regional_ori <- ifelse(DF$regions_ori==1, 1, 0)
DF$pluriregional_ori <- ifelse(DF$regions_ori>1 & DF$regions_ori<5, 1, 0)
DF$multiregional_ori <- ifelse(DF$regions_ori>4, 1, 0)

Data <- DF %>% group_by(Time.Period) %>% 
  summarise(Regional = sum(abs_norms>0 & regional_ori==1)/n(),
            Pluriregional = sum(abs_norms>0 & pluriregional_ori==1)/n(),
            Multiregional = sum(abs_norms>0 & multiregional_ori==1)/n())


Data2 <- melt(Data, id.vars = "Time.Period", variable.name = "Regionality",
              value.name="Number") 


g <- ggplot(Data2, aes(y=Number, x=Time.Period, fill =`Regionality`)) +
  scale_y_continuous(label = scales::percent) +
  geom_bar(position="stack", stat="identity")+
  labs(title = "", x = "", y = "") +
  theme_minimal()+
  ggtitle("Regionality") +
  scale_fill_manual(values = c("#11A579","#E73F74", "#F2B701"), 
                    labels = c('1 continent', '2-4 continents', '5 continents')) +
  theme(legend.position="bottom",
        legend.key.width = unit(0.2, "cm"), 
        legend.key.height = unit(0.2, "cm"),
    legend.text = element_text(size = 5), legend.title = element_blank(), 
        axis.title.y= element_text(size = 10),  axis.text.y = element_text(size = 6), axis.text.x = element_text(size = 6, angle = 45, hjust=1), plot.title = element_text(size = 8, face = "bold"))
g

Data3 <- DF %>% group_by(Time.Period) %>% 
  summarise(Pollution = sum(abs_norms>0 & Pollution==1)/n(),
            Fish = sum(abs_norms>0 & Fish==1)/n(),
            Freshwater = sum(abs_norms>0 & Freshwater==1)/n(),
            Ocean = sum(abs_norms>0 & Ocean==1)/n(),
            Biodiversity = sum(abs_norms>0 & Biodiversity==1)/n(),
            Agriculture = sum(abs_norms>0 & Agriculture==1)/n(),
            General = sum(abs_norms>0 & General==1)/n(),
            Energy = sum(abs_norms>0 & Energy==1)/n(),
            Weapons = sum(abs_norms>0 & Weapons==1)/n(),
            Others = sum(abs_norms>0 & Others==1)/n())

Data4 <- melt(Data3, id.vars = "Time.Period", variable.name = "Subjects",
              value.name="Number") 

Palette = c("#7F3C8D","#11A579","#3969AC","#F2B701","#E73F74","#80BA5A","#E68310","#008695","#CF1C90","#f97b72","#4b4b8f")


g2 <- ggplot(Data4, aes(y=Number, x=Time.Period, fill =`Subjects`)) +
  scale_y_continuous(label = scales::percent) +
  geom_bar(position="stack", stat="identity", width = 0.7)+
  labs(title = "", x = "", y = "") +
  theme_minimal()+
  ggtitle("Subject areas") +
  scale_fill_manual(values= Palette) +
  theme(legend.position="bottom",
        legend.key.width = unit(0.2, "cm"), 
        legend.key.height = unit(0.2, "cm"),
    legend.text = element_text(size = 5), legend.title = element_blank(), 
        axis.title.y= element_text(size = 10),  axis.text.y = element_text(size = 6), axis.text.x = element_text(size = 6, angle = 45, hjust=1), plot.title = element_text(size = 8, face = "bold"))
g2

g3 <- ggarrange(g, g2, ncol = 2, align = "hv")
g3


ggsave(
  filename = "Figure 1.tiff",
  bg = "white",
  plot = g3,
  device = "tiff",       
  width = 6.5,             
  height = 3,              
  dpi = 600                
)
